package cn.caishen.report.common.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

/**
 * @author LB
 */
public class DateUtil {

    /**
     * 时间转string
     *
     * @param date
     * @return
     */
    public static String dateTime2String(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * 日期转string
     *
     * @param date
     * @return
     */
    public static String date2String(Date date) {
        return date2String(date, "yyyy-MM-dd");
    }

    public static String date2String(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * string转日期
     *
     * @param string
     * @return
     */
    public static Date string2Date(String string) {
        if (StringUtil.isEmpty(string)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * string转时间
     *
     * @param string
     * @return
     */
    public static Date string2DateTime(String string) {
        if (StringUtil.isEmpty(string)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static Date getNowDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getNowTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 格式化前端传入的日期
     *
     * @param date
     * @return
     */
    public static Date formatDate(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(dateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 时间戳转日期
     *
     * @param timestamp
     * @return
     */
    public static Date timestamp2Date(Long timestamp) {
        Timestamp t = new Timestamp(timestamp);
        return new Date(t.getTime());
    }


    /**
     * 两个日期比较
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean dateEquals(Date date1, Date date2) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date newDate1 = dateFormat.parse(dateFormat.format(date1));
            Date newDate2 = dateFormat.parse(dateFormat.format(date2));
            return newDate1.equals(newDate2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 日期转时间戳
     *
     * @param date
     * @return
     */
    public static Long Date2Timestamp(Date date) {
        return date.getTime();
    }

    /**
     * 获取当前时间用于文件名称，格式 yyyyMMddHHmm
     *
     * @return
     */
    public static String getNowTime2File() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        return dateFormat.format(new Date());
    }

    /**
     * 传入String类型日期(ISO8601标准时间:yyyy-MM-dd'T'HH:mm:ss.SSS'Z')，返回字符串类型时间(yyyy-MM-dd)
     */
    public static String getDateStrFromISO8601Timestamp(String ISOdate, String format) {
        if (StringUtil.isEmpty(ISOdate)) {
            return "";
        }
        DateTimeFormatter ISODateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        DateTime dateTime = ISODateTimeFormatter.parseDateTime(ISOdate);
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
        return dateTime.toString(dateTimeFormatter);
    }

    /**
     * 传入String类型日期(ISO8601标准时间:yyyy-MM-dd'T'HH:mm:ss.SSS'Z')，返回字符串类型时间(yyyy-MM-dd)
     */
    public static String getDateStrFromISO8601Timestamp(String ISOdate, String dateFormat, String format) {
        if (StringUtil.isEmpty(ISOdate)) {
            return "";
        }
        DateTimeFormatter ISODateTimeFormatter = DateTimeFormat.forPattern(dateFormat);
        DateTime dateTime = ISODateTimeFormatter.parseDateTime(ISOdate);
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
        return dateTime.toString(dateTimeFormatter);
    }

    /**
     * 判断是否是日期
     *
     * @param date
     * @return
     */
    public static boolean isDate(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dateFormat.parse(date);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取多少分钟之后的时间
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date getTimeInMillis(Date date, int minute) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        long time = date.getTime();
        time += minute * 60 * 1000;
        return longToDate(time);
    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @return
     */
    public static Date longToDate(Long time) {
        int length = time.toString().length();
        if (length == 10) {
            return new Date(time * 1000);
        } else {
            return new Date(time);
        }
    }

    /**
     * 获取年份
     *
     * @return
     */
    public static Integer getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 日期比较，
     * date1大于date2 --> true
     * date1小于date2 --> false
     * date1等于date2 --> true
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean dateCompare(Date date1, Date date2) {
        if (Objects.isNull(date1)) {
            return false;
        }

        if (dateEquals(date1, date2)) {
            return true;
        }

        return date1.after(date2);
    }

    /**
     * 获取差异日期，传入日期，差异天数
     *
     * @param date
     * @param diffDays
     * @return
     */
    public static Date getDiffDate(Date date, int diffDays) {
        if (Objects.isNull(date)) {
            return null;
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, diffDays);
        return calendar.getTime();
    }

    /**
     * 获取15天之前的日期
     *
     * @param date
     * @return
     */
    public static Date get15DiffDate(Date date) {
        Date diffDate = getDiffDate(date, -15);
        if (!dateCompare(diffDate, new Date())) {
            diffDate = new Date();
        }
        return diffDate;
    }

    public static void main(String[] args) {
        String diff = "2022-01-06T11:15:11.888Z";
        String dateStrFromISO8601Timestamp = getDateStrFromISO8601Timestamp(diff, "yyyy-MM-dd HH:mm:ss");
        System.out.println(dateStrFromISO8601Timestamp);
    }
}
